
// 审核表格数据仓库
import router from '@/router/index'
import { Store, Mutation } from 'vuex'
import {creatEnum, deepCopy} from '@/utils/tools'
import {

    changeTabelToolsModel, 
    tableSearchBarSwitch,
    updateTable,

} from '@/components/auditing_tools/index'

import auditingTableConf from '@/components/ivew_conf/auditing/auditing_table_conf'
import AuditingReqParams from '@/components/ivew_conf/auditing/auditing_req_params'
import auditingReqParams from '../../components/ivew_conf/auditing/auditing_req_params';


import { filter as formBtnsConf } from '@/components/organization_tools/filter'
import * as formComponents from '@/components/ivew_conf/organization/form_mod_conf'
import departmentDetailed from '../../components/ivew_conf/organization/department_detailed';
import {powerStatus} from '@/components/ivew_conf/power/index'

class stateInf {

    
    toolsModel:tableInf.toolsMod
    toolsModels:any
    toolsContainerComponent:string
    toolsFunConf:commonInf.toolsBarItem[]
    tableReqParams:AuditingReqParams
    
    tableOrders:carTableOrder[];

    tabeTitleConf:tableInf.tableTitle[]
    selectedRow:any
    foldingLayer:tableInf.flodKey
    autoOpenFoldingLayer:boolean

    controlBtns:any[]

    componentStore:formInf.mod[]

    currentFormData: any

    loading: boolean
}


class auditingStore {

    namespaced: true
    
    state:stateInf = {

        // 工具条当前模式
        toolsModel: 'close', 

        // 工具条模式可切换关键字
        toolsModels: creatEnum(['open', 'close']),
        
        // 工具条操作容器当前加载组件
        toolsContainerComponent: 'c-auditing-search-bar',

        // 工具条操作面板模块配置
        toolsFunConf: [
            
            changeTabelToolsModel,
            tableSearchBarSwitch,
            updateTable

        ],

        // 表格数据列表请求参数
        tableReqParams: new AuditingReqParams(), 

        // 表格数据列表
        tableOrders: [],

        // 表格表头配置
        tabeTitleConf: auditingTableConf,
        
        // 被选中行数据
        selectedRow: {jsonData:{}},

        // 是否显示折叠层 
        foldingLayer: 'close',

        // 点击表格项，自动展开折叠层
        autoOpenFoldingLayer: true,

        // 表单控制按键
        controlBtns: [],

        // 表单加载组件
        componentStore: [ formComponents.personal ],
        
        // 当前表单所需数据 (保证数据结构的完整性)
        currentFormData: {},

        // 是否加载中
        loading: false

    }

    mutations = {
        
        // 切换折叠层
        foldingLayerSwitch(state:stateInf, data?:tableInf.flodKey){
            let newStatus:tableInf.flodKey = state.foldingLayer === 'open' ? 'close' : 'open';
                state.foldingLayer = data || newStatus;

                // 清除url缓存参数(防止刷新后， 再次进入表单界面)
                if(state.foldingLayer === 'close'){
                    router.replace({query:{}})
                }
        },
        

        // 更新车险表格数据查询参数
        updateTableReqParams(state:stateInf, data:auditingReqParams){
            state['tableReqParams'] = data;
        },


        // 设置表格选中项数据
        setSelectedRow(state:stateInf, row:carTableOrder){
            state.selectedRow = row;
        },
        
        // 缓存表格列表数据
        updateTableOrders(state, orders){
            state.tableOrders = orders || [];
        },
        
        // 设置工具条控制面板加载组件
        setToolsContainerComponent(state:stateInf, componentName:string){
            state.toolsContainerComponent = componentName;
        },

        // 工具条模式切换
        setToolsModel (state, type?:string){
            const mod:string = state['toolsModel'];
            let Models = state.toolsModels;
            let num:number = Models[mod];
            num+=1;
            
            state['toolsModel'] = type ||  Models[num] || Models[0];
        }, 

        // 更新表单加载按键
        updateControlBtns ( state, conf:any[]){
            state['controlBtns'] = conf
        },

        // 更新加载表单组件
        updateComponentStore (state, type:string){
            
            let map = {
                company: 'department',
                department: 'department',
                personal: 'personal'
            };

            state['componentStore'] = [ formComponents[map[type]] ]
        }
        
    }

    getters = {
        
        // 当前折叠层状态
        foldingLayerState (state:stateInf):boolean{
            return state.foldingLayer === 'open';
        },

        // 部门表单组件配置
        departmentMods (steta, getters, rootState){
            return rootState['modConf']['departmentForm']['readonly'];
        },

        // 人员表单组件配置人员表单组件配置
        personalMods (state, getters, rootState ){
            return rootState['modConf']['personalForm']['readonly'];
        },

    }

    actions = {


        // 表格选中项操作分配
        tableSelect ({ state, dispatch, commit }, {type, data}){

            const map = {
                clear: 'clearSelectedRow',
                update: 'updateSelectedRow'
            };

            dispatch(map[type], data)
        },  
        

        // 请求表单数据
        formDataIssus ({state, dispatch, commit}, type){

            let  currentFormData = state.selectedRow['jsonData']
            let map = {
                company: () =>{
                    state['currentFormData'] = currentFormData || {}
                },
                department: () =>{
                    state['currentFormData'] = currentFormData || {}
                },
                personal: () =>{

                    let cache = new powerStatus()
                    
                    if(currentFormData.acl){

                        currentFormData.acl.forEach(key =>{
                                cache[key] = true;
                        })
                        
                    }

                    currentFormData['acl'] = cache;
                    state['currentFormData'] = currentFormData;
                    
                }
            };

            map[type]()

        },
        

        // 提交选中项数据
        updateSelectedRow({state, commit, dispatch}, row){

            let type = row['_describe']['type']['key']

            // 缓存当前选中行数据
            commit('setSelectedRow', row)

            // 请求表单展示数据
            dispatch('formDataIssus', type)
            
            // 配置加载表单
            commit('updateComponentStore', type)
            
            // 配置表单加载按键
            commit('updateControlBtns', formBtnsConf(row['result']))
            
            // 打开折叠层
            commit('foldingLayerSwitch', 'open')

          
        },

        // 清空选中仓库
        clearSelectedRow({state, commit, dispatch}){

            commit('setSelectedRow', {})
            commit('foldingLayerSwitch', 'close')
            
        },  

        // 初始化
        initSet({state, commit, dispatch}){
            let {company, department} = this['state']['mainStore']['userBase']
            let tableReqParams = new AuditingReqParams()
            tableReqParams =  Object.assign(deepCopy(tableReqParams), {
                company,
                department,
            })
            
            // 设置初始会员所属部门
            commit('updateTableReqParams', tableReqParams)

            // 请求列表数据
            dispatch('request', {
                url: 'get/auditings',
                data: {}
            },{root:true})
        },
        
        
    }


}



export default auditingStore; 
